﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySpace.Data;
using DataBaseTable;

public partial class Content_list : System.Web.UI.Page
{
    protected static int nowpages;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            nowpages = 1;
            DataBd();
            if (!string.IsNullOrEmpty(Request.QueryString["bigclass"]) && Request.QueryString["bigclass"] == "4")
            {
                bdsubclass();
                Panel_zhidujianshe.Visible = true;
            }
        }
    }
    protected void bdsubclass()
    {
        DBHandler dbh = GetHandler.Init();
        dbh.CommandText = "select * from News_small_type where forbigclass=4";
        subclass.DataSource = dbh.ExecuteDataTable();
        subclass.DataBind();
    }
    protected void DataBd()
    {
        DBHandler dbh = GetHandler.Init();
        string newsclass = string.Empty;
        newsclass += string.IsNullOrEmpty(Request.QueryString["bigclass"]) ? "" : "BigClassId=" + Request.QueryString["bigclass"] + " ";
        newsclass += string.IsNullOrEmpty(Request.QueryString["smallclass"]) ? "" : string.IsNullOrEmpty(newsclass.Trim()) ? "SmallClassId=" + Request.QueryString["smallclass"] : " and " + "SmallClassId=" + Request.QueryString["smallclass"];
        newsclass += string.IsNullOrEmpty(Request.QueryString["searchkeyword"]) ? "" : "NewTitle like '%" + Request.QueryString["searchkeyword"] + "%'";
        newsclass = string.IsNullOrEmpty(newsclass.Trim()) ? "" : "where " + newsclass;
        dbh.CommandText = string.Format("select * from news {0} order by  IsTop desc,Sort desc,id desc", newsclass);
        string note = "<span style='  margin-left:10px; margin-right:10px;'>→</span>";
        if (string.IsNullOrEmpty(newsclass))
        {
            theclass.Text = "<span style='  margin-left:10px; margin-right:10px;'>→</span>新闻资讯";
            Master.WebSubTitle += "新闻资讯 - ";
        }
        else
        {

            if (!string.IsNullOrEmpty(Request.QueryString["smallclass"]))
            {
                SmallClassTable sm = new SmallClassTable(Convert.ToInt32(Request.QueryString["smallclass"]));
                theclass.Text += note + sm.ForBigClassName;
                theclass.Text += note + sm.SmallClassName;
                Master.WebSubTitle += sm.SmallClassName + " - ";
            }
            else if (!string.IsNullOrEmpty(Request.QueryString["bigclass"]))
            {
                Master.WebSubTitle += new BigClassTable(Convert.ToInt32(Request.QueryString["bigclass"])).bigclassname;
                theclass.Text = note + Master.WebSubTitle;
                Master.WebSubTitle += " - ";
            }
            if (!string.IsNullOrEmpty(Request.QueryString["searchkeyword"]))
            {
                theclass.Text = note + "查找结果";
            }
        }
        PagedDataSource ps = new PagedDataSource();
        ps.DataSource = dbh.ExecuteDataTable().DefaultView;
        ps.AllowPaging = true;
        ps.PageSize = 20;
        toone.Enabled = true;
        toup.Enabled = true;
        tonext.Enabled = true;
        tolast.Enabled = true;
        if (nowpages == 1)
        {
            toone.Enabled = false;
            toup.Enabled = false;
        }
        if (nowpages == ps.PageCount)
        {
            tonext.Enabled = false;
            tolast.Enabled = false;
        }
        if (!IsPostBack)
        {
            for (int i = 1; i <= ps.PageCount; i++)
            {
                jumppage.Items.Add(i.ToString());
            }
        }
        Label1.Text = nowpages.ToString();
        Label2.Text = ps.PageCount.ToString();
        tpagesizes.Text = ps.PageSize.ToString();
        pagesum.Text = ps.DataSourceCount.ToString();
        ps.CurrentPageIndex = nowpages - 1;//获取当前页码
        Repeater1.DataSource = ps;
        Repeater1.DataBind();
    }
    protected void toone_Click(object sender, EventArgs e)
    {
        nowpages = 1;
        DataBd();
    }
    protected void toup_Click(object sender, EventArgs e)
    {
        --nowpages;
        DataBd();
    }
    protected void tonext_Click(object sender, EventArgs e)
    {
        ++nowpages;
        DataBd();
    }
    protected void jumppage_SelectedIndexChanged(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(((DropDownList)sender).SelectedValue);
        DataBd();
    }
    protected void tolast_Click(object sender, EventArgs e)
    {
        nowpages = Convert.ToInt32(Label2.Text);
        DataBd();
    }
    protected string TSubstring(string HTMLStr)
    {
        return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");

    }
}